package mk.jug.academy.controller.support;

import java.util.List;

import javax.servlet.jsp.JspWriter;

import mk.jug.academy.model.Event;
import mk.jug.academy.service.EventService;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

/**
 * Custom tag to render the event section on each page.
 *
 * @author Ice.Penov
 */
public class EventsTag extends RequestContextAwareTag {

	private static final long serialVersionUID = -6574400815460573123L;

	@Override
	protected int doStartTagInternal() throws Exception {
		EventService eventService = getRequestContext().getWebApplicationContext().getBean(EventService.class);
		List<Event> events = eventService.list(1, 5).getItems();
		JspWriter out = pageContext.getOut();
		
		out.println("<div class=\"spacer5\" style=\"float:left\">&nbsp;</div>");
		out.println("<div style=\"float:left; width:205px\">");
		for (int i = 0; i < events.size(); i++) {
			Event event = events.get(i);
			if (i == events.size() - 1) {
				out.println("<div class=\"lastActivityItem\">" + event.getMessage() + "</div>");
			} else {
				out.println("<div class=\"activityItem\">" + event.getMessage() + "</div>");
			}
		}
		return EVAL_PAGE;
	}

}